/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn;

import au.edu.swin.unilearn.subject.*;
import au.edu.swin.unilearn.tutorialGroup.*;
import au.edu.swin.unilearn.universitystaff.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author DidierLeung
 */
public class SetSubjectBean
{
    @EJB
    private SubjectSessionRemote subjectSession;
    @EJB
    private UniversityStaffSessionRemote universityStaffSession;
    @EJB
    private TutorialGroupSessionRemote tutorialGroupSession;
    
    private String subjectCode;
    private String subjectName;
    private Integer noOfTutorialClass;
    private List<TutorialClass> tutorialClassList;
    private String errorMsg;

    /** Creates a new instance of SetSubjectBean */
    public SetSubjectBean()
    {
    }

    public String getSubjectCode()
    {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode)
    {
        this.subjectCode = subjectCode;
    }

    public String getSubjectName()
    {
        return subjectName;
    }

    public void setSubjectName(String subjectName)
    {
        this.subjectName = subjectName;
    }

    public Integer getNoOfTutorialClass()
    {
        return noOfTutorialClass;
    }

    public void setNoOfTutorialClass(Integer noOfTutorialClass)
    {
        this.noOfTutorialClass = noOfTutorialClass;
    }

    private List<SelectItem> getAllStaffs()
    {
        List<SelectItem> selectStaffList = new ArrayList();
        selectStaffList.add(new SelectItem("Select one Staff", "Select one Staff"));

        for (UniversityStaff staff : universityStaffSession.getAllUniversityStaffs())
        {
            SelectItem si = new SelectItem(staff.getId(), staff.getId() + " - " + staff.getName());
            selectStaffList.add(si);
        }
        return selectStaffList;
    }

    public String setTutorialClass()
    {
        tutorialClassList = new ArrayList();
        for (int i = 0; i < noOfTutorialClass; i++)
        {
            TutorialClass tc = new TutorialClass((i + 1), getAllStaffs());
            tutorialClassList.add(tc);
        }
        setErrorMsg("");
        return "next";
    }

    public String registerSubject()
    {
        if(!checkStaffAllocation())
        {
            return "error";
        }
        
        try
        {
            //Register new Subject to Database
            Subject s = new Subject(subjectCode, subjectName);
            subjectSession.addSubject(s);
            
            //Allocate each tutorial class to a staff
            for(TutorialClass tc : tutorialClassList)
            {
                UniversityStaff staffObj = universityStaffSession.getUniversityStaff(tc.getStaffID());
                TutorialGroup tg = new TutorialGroup(tc.getTutorialClassNo(), s, staffObj);
                tutorialGroupSession.addTutorialGroup(tg);
            }
            return "success";
        }
        catch (Exception e)
        {
            return "failure";
        }
    }

    public void validateSubjectCode(FacesContext context, UIComponent toValidate, Object obj)
    {
        String subCode = (String) obj;
        Boolean isSubjectExist = subjectSession.isSubjectExist(subCode);
        if (isSubjectExist)
        {
            ((UIInput) toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Subject with code " + subCode + " is already in the database");
            context.addMessage(toValidate.getClientId(context), message);
        }
    }

    public List<TutorialClass> getTutorialClassList()
    {
        return tutorialClassList;
    }

    public void setTutorialClassList(List<TutorialClass> tutorialClassList)
    {
        this.tutorialClassList = tutorialClassList;
    }

    private boolean checkStaffAllocation()
    {
        for (TutorialClass tc : tutorialClassList)
        {
            if ((tc.getStaffID()).equals("Select one Staff"))
            {
                setErrorMsg("One staff must be allocated to this tutorial class");
                return false;
            }
        }
        return true;
    }

    public String getErrorMsg()
    {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg)
    {
        this.errorMsg = errorMsg;
    }
}
